/*
Copyright 2020 yozora.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
	"github.com/fatih/color"
	"math/rand"
	"strconv"
	"time"
)

var random = rand.New(rand.NewSource(time.Now().Unix()))

const (
	red     = color.FgRed
	green   = color.FgGreen
	yellow  = color.FgYellow
	blue    = color.FgBlue
	magenta = color.FgMagenta
	cyan    = color.FgCyan
	white   = color.FgWhite
)

var colors = map[color.Attribute]color.Attribute{
	red:     color.FgHiRed,
	green:   color.FgHiGreen,
	yellow:  color.FgHiYellow,
	blue:    color.FgHiBlue,
	magenta: color.FgHiMagenta,
	cyan:    color.FgHiCyan,
	white:   color.FgHiWhite,
}

var colorNames = map[color.Attribute]string{
	red:     "Red",
	green:   "Green",
	yellow:  "Yellow",
	blue:    "Blue",
	magenta: "Magenta",
	cyan:    "Cyan",
	white:   "White",
}

const (
	grass   = ' '
	blocker = '■'
)

const (
	TileGrass TileType = iota
	TileBlocker
)

type TileType int

type Tile struct {
	Type   TileType
	Marker rune
}

//构成 Spirit 的组成图块
type TrailSegment struct {
	//图块字符
	Marker rune
	Pos    Position
	Color  color.Attribute
}

type SpiritObject struct {
	Name      string
	CreatedAt time.Time
}

//type SpiritInterface interface {
//	Update(g *Game, delta float64)
//}

type Position struct {
	X float64
	Y float64
}

func PositionFromInt(x, y int) Position {
	return Position{float64(x), float64(y)}
}

func (p Position) RoundX() int {
	return int(p.X + 0.5)
}

func (p Position) RoundY() int {
	return int(p.Y + 0.5)
}

func (p Position) toKey() string {
	return strconv.Itoa(p.RoundX()) + "-" + strconv.Itoa(p.RoundY())
}
